package de.LooLo;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Displays compass that points to nearest loo
 * @author Lukas Siedel 
 *
 */
public class CompassActivity extends Activity implements SensorEventListener, LocationListener {

	SensorManager sensorManager;
	Sensor sensor;
	
	/**
	 * Compass Needle to spin
	 */
	ImageView needleView;
	TextView errorView;
	Bitmap compassBitMap;
	Bitmap needleBitMap;
	Location nearest = new Location("nearest");
	Matrix matrix;
	float northDegree;
	Location currentLoc = null;
	float distance=-1;
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.compass);
		
		needleView = (ImageView) findViewById(R.id.needle);
		errorView = (TextView) findViewById(R.id.error_text);
		
		double lat = this.getIntent().getDoubleExtra("lat", 0);
		double lon = this.getIntent().getDoubleExtra("lon", 0);
		nearest.setLatitude(lat);
		nearest.setLongitude(lon);
		
		if(lat != 0 && lon != 0){
		northDegree = getCurrentLocation(this).bearingTo(nearest);
		sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
		sensorManager.registerListener(this, sensor,
				SensorManager.SENSOR_DELAY_UI);


		needleBitMap = BitmapFactory.decodeResource(getResources(),
				R.drawable.needle);
		matrix = new Matrix();
		
		
		float chevron=0;
			matrix.setRotate(northDegree-chevron);

			compassBitMap = Bitmap.createBitmap(needleBitMap, 0, 0,
					needleBitMap.getWidth(), needleBitMap.getHeight(), matrix,
					true);
			needleView.setImageBitmap(compassBitMap);
		}else{
			needleView.setVisibility(View.GONE);
			errorView.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
				
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		float change=0;
		if (event.sensor == sensor) {
			float chevron = event.values[0];
			change= change + chevron;
			if(change>5){			
			matrix.setRotate(northDegree-change);

			compassBitMap = Bitmap.createBitmap(needleBitMap, 0, 0,
					needleBitMap.getWidth(), needleBitMap.getHeight(), matrix,
					true);
			needleView.setImageBitmap(compassBitMap);
			change=0;
			}
		}

	}

	@Override
	public void onLocationChanged(Location location) {
		if((currentLoc.distanceTo(location)-distance)>2 ||(currentLoc.distanceTo(location)-distance)<-2){
			distance=currentLoc.distanceTo(location);
			currentLoc=location;
			northDegree = location.bearingTo(nearest);
		}
		
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}
	LocationManager locationManager;
	String context;
	
	/**
	 * @return current location
	 */
	public Location getCurrentLocation(Context mContext) {
		context = Context.LOCATION_SERVICE;
		locationManager = (LocationManager) mContext.getSystemService(context);
		String provider = LocationManager.GPS_PROVIDER;
		locationManager.requestLocationUpdates(provider, 0, 0, this);
		currentLoc = locationManager.getLastKnownLocation(provider);

		return currentLoc;
	}
	@Override
	public void onBackPressed() {
		System.exit(0);
		//finish();
	}
}
